﻿package org.hyy.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;

import org.hyy.base.DatabaseConnection;
import org.hyy.util.StringUtil;
import org.hyy.vo.Pay;
import org.hyy.vo.User;

public class UserDAO {
	private DatabaseConnection dbc;
	private Connection con;				//数据库连接对象
	private PreparedStatement ps;		//预编译对象，用来执行SQL语句
	private ResultSet rs;
	
	/**
	 * 添加用户
	 * @param user
	 * @return
	 */
	public boolean insertOne(User user){
		boolean flag = false;
		//打开数据库
		dbc = new DatabaseConnection();
		con = dbc.getCon();
		//操作数据库
		String sql = "insert into user values(null,?,?,?,?,?)";
		try {
			ps = con.prepareStatement(sql);
			
			//设置参数
			ps.setString(1, user.getUsername());
			ps.setString(2, StringUtil.getMD5(user.getPassword().getBytes()));
			ps.setString(3, user.getPhone());
			ps.setString(4, user.getAddr());
			ps.setTimestamp(5, new Timestamp(user.getRdate().getTime()));
			
			//执行操作
			flag = ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//关闭数据库
			dbc.close(con);
			dbc = null;
		}

		return flag;
	}
	
	public boolean changeUserById(User user){
		boolean flag = false;
		//打开数据库
		dbc = new DatabaseConnection();
		con = dbc.getCon();
		//操作数据库
		String sql = "update user set username=?, password=?, phone=?, addr=? where id=?";
		try {
			ps = con.prepareStatement(sql);
			
			//设置参数
			ps.setString(1, user.getUsername());
			ps.setString(2, StringUtil.getMD5(user.getPassword().getBytes()));
			ps.setString(3, user.getPhone());
			ps.setString(4, user.getAddr());
			ps.setInt(5, user.getId());
			
			//执行操作
			flag = ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//关闭数据库
			dbc.close(con);
			dbc = null;
		}

		return flag;
	}
	
	public boolean login(String username,String password){
		boolean flag = false;
		//打开数据库
		dbc = new DatabaseConnection();
		con = dbc.getCon();
		//操作数据库
		String sql = "select * from user where username = ? and password = ?";
		try {
			ps = con.prepareStatement(sql);
			
			//设置参数
			ps.setString(1, username);
			ps.setString(2, StringUtil.getMD5(password.getBytes()));
			
			//执行操作
			rs = ps.executeQuery();
			flag = rs.next();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//关闭数据库
			dbc.close(con);
			dbc = null;
		}
		
		return flag;
	}
	
	public boolean delete(int uid){
		boolean flag = false;
		//打开数据库
		dbc = new DatabaseConnection();
		con = dbc.getCon();
		//操作数据库
		String sql = "delete from user where id=?";
		try {
			ps = con.prepareStatement(sql);
			
			//设置参数
			ps.setInt(1, uid);
			
			//执行操作
			ps.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//关闭数据库
			dbc.close(con);
			dbc = null;
		}
		
		return flag;
	}
	
	public User getUserinfoByName(String username){
		//打开数据库
		dbc = new DatabaseConnection();
		con = dbc.getCon();
		User user = null;
		//操作数据库
		String sql = "select * from user where username = ?";
		try {
			ps = con.prepareStatement(sql);
			
			//设置参数
			ps.setString(1, username);
			
			//执行操作
			rs = ps.executeQuery();
			while(rs.next()){
				user = new User();
				user.setId(rs.getInt(1));
				user.setUsername(rs.getString(2));
				user.setPassword(rs.getString(3));
				user.setPhone(rs.getString(4));
				user.setAddr(rs.getString(5));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			//关闭数据库
			dbc.close(con);
			dbc = null;
		}
		
		return user;
	}
}
